'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async say() {
    this.ctx.body = 'hello word';
  }
  async login() {
    const { name, pwd } = this.ctx.query;
    console.log(name, pwd);
    this.ctx.body = { name, pwd };
  }
  async getAccount() {
    const { name, pwd } = this.ctx.request.body;
    console.log(name, pwd);
    this.ctx.body = { name, pwd };
  }
  async getUser() {
    const { ctx } = this;
    const user = await ctx.service.user.getUser();
    this.ctx.body = user;
  }
  async addUser() {
    const { ctx } = this;
    const { name, pwd } = ctx.request.body;
    const res = await ctx.service.user.addUser(name, pwd);
    if (res.affectedRows === 1) {
      ctx.body = { code: 0, msg: '成功' };
    } else {
      ctx.body = { code: 1, msg: '失败' };
    }
  }
  async delUserById() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const res = await ctx.service.user.delUserById(id);
    if (res.affectedRows === 1) {
      ctx.body = { code: 0, msg: '成功' };
    } else {
      ctx.body = { code: 1, msg: '失败' };
    }
  }
  async getDataById() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const res = await ctx.service.user.getDataById(id);
    ctx.body = res[0];
  }
  async edit() {
    const { ctx } = this;
    const { id, name, pwd } = ctx.request.body;
    const res = await ctx.service.user.edit(id, name, pwd);
    if (res.affectedRows === 1) {
      ctx.body = { code: 0, msg: '成功' };
    } else {
      ctx.body = { code: 1, msg: '失败' };
    }
  }
}

module.exports = HomeController;
